const baseUrl = "http://printer.jxwazx.cn"

function request(method, url, params) {
    wx.showNavigationBarLoading()
    wx.showLoading({
        title: '加载中',
    })
    return new Promise((reslove, reject) => {
        // console.log(wx.getStorageSync('token'));
        wx.request({
            url: baseUrl + url,
            data: params,
            header: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + wx.getStorageSync('token')
            },
            method: method,
            success: (res) => {
                // console.log(res);
                if (res.statusCode == 200) {
                    reslove(res)
                    // console.log(res);
                } else {
                    reject(res.message)
                }
            },
            fail: (res) => {
                console.log(res);
            },
            complete: () => {
                setTimeout(() => {
                    wx.hideLoading()
                }, 100)
            }
        })
    })
}

const getRequest = (url, params) => {
    return request('get', url, params)
}
const putRequest = (url, params) => {
    return request('put', url, params)
}
const deleteRequest = (url, params) => {
    return request('delete', url, params)
}

module.exports = {
    request,
    getRequest,
    putRequest,
    deleteRequest
}